package com.sgu6.sgu6_machine.ui.fragment.set;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.ui.views.AmountView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DoorSetF extends BaseF {

    @BindView(R.id.atCloseDoorTime)
    AmountView atCloseDoorTime;
    @BindView(R.id.atSendValleyTime)
    AmountView atSendValleyTime;
    @BindView(R.id.ll_send_valley)
    LinearLayout llSendValley;

    public static DoorSetF newInstance() {
        return new DoorSetF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_other, container, false);
        unbinder = ButterKnife.bind(this, v);
        initUI();
        return v;
    }

    public void initUI() {
        atCloseDoorTime.setDefaultValue(20);
        atSendValleyTime.setDefaultValue(120);
        if (!MachineContainer.machineConfigDto.withPack) {
            llSendValley.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btnSetCloseDoorTime, R.id.btnSetSendValleyTime})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnSetCloseDoorTime:
                String co = String.format(MachineCmdEnum.关门延时时间.code(), atCloseDoorTime.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(co));
                break;
            case R.id.btnSetSendValleyTime:
                String cs = String.format(MachineCmdEnum.送谷保持时间.code(), atSendValleyTime.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(cs));
                break;
        }
    }
}
